package com.tckdq.tcc.web.controller;

import com.tckdq.tcc.enums.ErrorCodeEnum;
import com.tckdq.tcc.enums.ErrorCodeEnum;
import com.tckdq.tcc.exception.GlobalException;
import com.tckdq.tcc.service.ILoginService;
import com.tckdq.tcc.domain.Login;
import com.tckdq.tcc.query.LoginQuery;
import com.tckdq.tcc.result.JsonResult;
import com.tckdq.tcc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    public ILoginService loginService;
    /*/uaa/login/common*/
    /*/uaa/login/common*/
    //登录
    @RequestMapping(value="/common",method= RequestMethod.POST)
    public JsonResult login(@RequestBody @Valid Login login){
        loginService.login(login);

        return JsonResult.success(login);
    }

    //注册
    @RequestMapping(value="/register",method= RequestMethod.POST)
    public JsonResult saveRegister(@RequestBody Login login){
        Long loginId = loginService.save(login);
        return JsonResult.success(loginId);
    }


    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody Login login){
        if(login.getId()!=null){
            loginService.updateById(login);
        }else{
            loginService.insert(login);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        loginService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(loginService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        /*try {
            int i =1/0;
        } catch (GlobalException e) {
            e.printStackTrace();
        }*/
        if (true){
            throw new GlobalException(ErrorCodeEnum.NAME_ERROR);
        }
        return JsonResult.success(loginService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody LoginQuery query){
        Page<Login> page = new Page<Login>(query.getPage(),query.getRows());
        page = loginService.selectPage(page);
        return JsonResult.success(new PageList<Login>(page.getTotal(),page.getRecords()));
    }
}
